package com.ruoyi.project.jxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jxy.domain.Intention;
import com.ruoyi.project.jxy.service.IIntentionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 意图Controller
 *
 * @author ruoyi
 * @date 2019-12-06
 */
@RestController
@RequestMapping("/jxy/intention")
public class IntentionController extends BaseController {
    @Autowired
    private IIntentionService intentionService;

    /**
     * 查询意图列表
     */
    @PreAuthorize("@ss.hasPermi('jxy:intention:list')")
    @GetMapping("/list")
    public TableDataInfo list(Intention intention) {
        startPage();
        /*QueryWrapper<Intention> queryWrapper = new QueryWrapper<Intention>();
        if (StringUtils.isNotBlank(intention.getCode())) {
            queryWrapper.like("code", intention.getCode());
    }
        if (StringUtils.isNotBlank(intention.getName())) {
        queryWrapper.like("name", intention.getName());
    }
    List<Intention> list = intentionService.list(queryWrapper);*/
        intention.setCreateUser(SecurityUtils.getUsername());
        List<Map> list = intentionService.listIntention(intention);
        return getDataTable(list);
    }

    /**
     * 导出意图列表
     */
    @PreAuthorize("@ss.hasPermi('jxy:intention:export')")
    @Log(title = "意图", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Intention intention) {
        QueryWrapper<Intention> queryWrapper = new QueryWrapper<Intention>(intention);
        List<Intention> list = intentionService.list(queryWrapper);
        ExcelUtil<Intention> util = new ExcelUtil<Intention>(Intention.class);
        return util.exportExcel(list, "intention");
    }

    /**
     * 获取意图详细信息
     */
    @PreAuthorize("@ss.hasPermi('jxy:intention:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(intentionService.getById(id));
    }

    /**
     * 新增意图
     */
    @PreAuthorize("@ss.hasPermi('jxy:intention:add')")
    @Log(title = "意图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Intention intention) {
        //意图校验
        if (StringUtils.isBlank(intention.getCode())) {
            return AjaxResult.error("意图英文标识为空");
        }
        if (intention.getSkillId() == null) {
            return AjaxResult.error("技能id为空");
        }
        if (StringUtils.isBlank(intention.getName())) {
            return AjaxResult.error("意图名称为空");
        }
        if (intention.getType() == null) {
            return AjaxResult.error("意图类型为空");
        }
        //校验技能下code 重复校验
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("code", intention.getCode());
        wrapper.eq("skill_id", intention.getSkillId());
        int count = intentionService.count(wrapper);
        if (count > 0) {
            return AjaxResult.error("意图英文标识已存在");
        }

        //构建状态
        intention.setStatus("0");
        intention.setCreateTime(new Date());
        intention.setUpdateTime(new Date());
        intention.setCreateUser(SecurityUtils.getUsername());
        intention.setUpdateUser(SecurityUtils.getUsername());
        return toAjax(intentionService.save(intention) ? 1 : 0);
    }

    /**
     * 修改意图
     */
    @PreAuthorize("@ss.hasPermi('jxy:intention:edit')")
    @Log(title = "意图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Intention intention) {
        if (intention.getSkillId() == null) {
            return AjaxResult.error("技能id为空");
        }
        //校验技能下code 重复校验
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("code", intention.getCode());
        wrapper.eq("skill_id", intention.getSkillId());
        wrapper.ne("id", intention.getId());
        int count = intentionService.count(wrapper);
        if (count > 0) {
            return AjaxResult.error("意图英文标识已存在");
        }
        intention.setCreateUser(SecurityUtils.getUsername());
        intention.setUpdateUser(SecurityUtils.getUsername());
        return toAjax(intentionService.updateById(intention) ? 1 : 0);
    }

    /**
     * 删除意图
     */
    @PreAuthorize("@ss.hasPermi('jxy:intention:remove')")
    @Log(title = "意图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(intentionService.removeIntentionByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
